package com.dhwl.hsh.merchant.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.webkit.URLUtil;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.bumptech.glide.Glide;
import com.dhwl.hsh.lib.bean.resp.BannerResponse;

/**
 * 首页广告banner图片所在的fragment
 *
 * @author Derek.Kidd
 * @time 2016/4/20 18:20
 */
public class BannerFragment extends Fragment {

    private static final String KEY_CONTENT = "BannerFragment";

    private BannerResponse bannerResponse;


    private int resId;

    public static BannerFragment newInstants(Context context, BannerResponse bannerResponse) {
        BannerFragment newPreFragment = new BannerFragment();

        Bundle bundle = new Bundle();
        bundle.putParcelable("banners", bannerResponse);
        newPreFragment.setArguments(bundle);
        return newPreFragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
            bannerResponse =   savedInstanceState.getParcelable(KEY_CONTENT);
        }else{
            Bundle bundle = getArguments();
            bannerResponse = bundle.getParcelable("banners");
        }

    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putParcelable(KEY_CONTENT, bannerResponse);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        ImageView imageView = new ImageView(getActivity());
        imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        //FIT_XY 把图片不按比例扩大/缩小到View的大小显示
        imageView.setScaleType(ScaleType.FIT_XY);

        if(URLUtil.isNetworkUrl(bannerResponse.getAdCode())){
            Glide.with(getActivity()).load(bannerResponse.getAdCode()).into(imageView);
        }
        return imageView;
    }
}
